package com.example.work.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.work.entity.UserEntity;
import com.example.work.service.UserService;
import com.example.work.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description:
 * @author: JinMu
 * @create: 2018-08-27 13:30
 * @Version: 1.0
 **/
@RestController
public class LoginController {

    @Autowired
    private UserService userService;

    @PostMapping("/login_con")
    public String login_con(@RequestBody JSONObject params, HttpServletRequest request) {
        String name = params.getString("name");
        String password = params.getString("password");
        String pwd = MD5Util.getmd32(password);
        UserEntity u = userService.queryuser(name);
        if (u != null && u.getPassword().equals(pwd) && u.getPassword() != "") {
            if (u.getKey() == null || u.getKey().equals("") || u.getMerchantID() == null || u.getMerchantID().equals("")) {
                return "succ_err";
            } else {
                request.getSession().setAttribute("user",u);
                return "success";
            }
        } else {
            return "error";
        }
    }

}
